package cn.samir.sudoku.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.samir.sudoku.R;
import cn.samir.sudoku.core.SettingItem;

public class SettingsListAdapter extends AdapterBase<SettingItem> {
	Context context;

	public SettingsListAdapter(Context context) {
		this.context = context;

	}

	@Override
	protected View getExView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.item_settings, null);

			viewHolder = new ViewHolder();
			viewHolder.container = (LinearLayout) convertView
					.findViewById(R.id.container);
			viewHolder.tv = (TextView) convertView.findViewById(R.id.tv);
			viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		SettingItem item = (SettingItem) getItem(position);
		// int width = parent.getWidth() / 3;
		// android.widget.AbsListView.LayoutParams layoutParams = new
		// android.widget.AbsListView.LayoutParams(
		// width, width);
		// viewHolder.container.setLayoutParams(layoutParams);
		if (item.resId != 0) {
			viewHolder.iv.setImageResource(item.resId);
		}
		viewHolder.tv.setText(item.text);
		return convertView;
	}

	@Override
	protected void onReachBottom() {

	}

	class ViewHolder {
		ImageView iv;
		TextView tv;
		LinearLayout container;
	}

	protected LayoutAnimationController getAnimationController() {
		int duration = 2000;
		AnimationSet set = new AnimationSet(true);

		Animation animation = new AlphaAnimation(0.0f, 1.0f);
		animation.setDuration(duration);
		set.addAnimation(animation);

		animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				-1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
		animation.setDuration(duration);
		set.addAnimation(animation);

		LayoutAnimationController controller = new LayoutAnimationController(
				set, 0.5f);
		controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
		return controller;
	}

}
